var arr = new Array();
$(document).ready(function () {
    // 打开页面后查询所有数据
    query()
    // 给查询按钮绑定事件

    $("#findBtn").click(doQuery)
    $("#expiretime_warn").click(expQuery)
    $("#inventory_warn").click(invQuery)


    $("#addBtn").click(function () {
        //iframe层
        parent.layer.open({
            type: 2,
            title: '添加商品信息',
            shadeClose: true,
            shade: 0.8,
            area: ['580px', '400px'],
            content: '/mgr/commodity/add.html', //iframe   的url
            end:function() {
                location.reload();
            }
        });
    })
})
function expQuery(){
    const id = $("#id").val()
    const expiretime = $("#expiretime").val()
    const purchaseprice = $("#purchaseprice").val()
    const price = $("#price").val()
    const create_time = $("#create_time").val()
    const modify_time = $("#modify_time").val()
    const isdel = $("#isdel").val()
    const inventory = $("#inventory").val()
    const cname = $("#cname").val()
    const batch = $("#batch").val()
    const expiretime_warn = "yes"
    // 清空表格
    $("#tbdy").empty()
    // 进行查询
    query(id,cname,batch,expiretime,purchaseprice,price,inventory,create_time,modify_time,isdel,expiretime_warn)

}
function invQuery(){
    const id = $("#id").val()
    const expiretime = $("#expiretime").val()
    const purchaseprice = $("#purchaseprice").val()
    const price = $("#price").val()
    const create_time = $("#create_time").val()
    const modify_time = $("#modify_time").val()
    const isdel = $("#isdel").val()
    const inventory = $("#inventory").val()
    const cname = $("#cname").val()
    const batch = $("#batch").val()
    const expiretime_warn = $("#expiretime_warn").val()
    const inventory_warn = "yes"

    // 清空表格
    $("#tbdy").empty()
    // 进行查询
    query(id,cname,batch,expiretime,purchaseprice,price,inventory,create_time,modify_time,isdel,expiretime_warn,inventory_warn)

}
// 获取查询条件，调用query() 函数进行查询
function doQuery() {
    const id = $("#id").val()
    const expiretime = $("#expiretime").val()
    const purchaseprice = $("#purchaseprice").val()
    const price = $("#price").val()
    const create_time = $("#create_time").val()
    const modify_time = $("#modify_time").val()
    const isdel = $("#isdel").val()
    const inventory = $("#inventory").val()
    const cname = $("#cname").val()
    const batch = $("#batch").val()
    const expiretime_warn = $("#expiretime_warn").val()
    const inventory_warn = $("#inventory_warn").val()
    // 清空表格
    $("#tbdy").empty()
    // 进行查询
    query(id,cname,batch,expiretime,purchaseprice,price,inventory,create_time,modify_time,isdel,expiretime_warn,inventory_warn)
}
function query(id,cname,batch,expiretime,purchaseprice,price,inventory,create_time,modify_time,isdel,expiretime_warn,inventory_warn) {
    $.ajax({
        url:'/mgr/commodity/list',//请求的url。在html中的/，表示从域名后面开始
        type:'get', //请求方式，GET或POST
        data:{ //要提交给服务器的数据
            id,
            cname,
            batch,
            expiretime,
            purchaseprice,
            price,
            inventory,
            create_time,
            modify_time,
            isdel,
            expiretime_warn,
            inventory_warn
        },
        dataType:'json',//服务器返回数据的类型
        success:function(data){//成功时执行该函数，data就是服务器返回的数据
            if(data.code==0){
                for(let item of data.data) {
                    // id,cname,batch,expiretime,purchaseprice,price,inventory,create_time,modify_time,isdel
                    let row = `
                                <tr>
                                    <td width="120">${item.cname}</td>
                                    <td width="180">${item.batch}</td>
                                    <td width="120">${item.expiretime}</td>
                                    <td width="120" style="text-align: center;">${item.purchaseprice}</td>
                                    <td width="120" style="text-align: center;">${item.price}</td>
                                    <td width="120" style="text-align: center;">${item.inventory}</td>
                                    <td width="200">
                                        <button type="button" class="btn btn-primary btn-xs   glyphicon glyphicon-edit" onclick="showById(${item.id})">修改</button>
                                        <button type="button" class="btn btn-xs btn-danger glyphicon glyphicon-trash" onclick="delById(${item.id})">删除</button>
                                    </td>
                                </tr>
                            `
                    $("#tbdy").append(row)
                }
            }else{
                $("#msg").html(data.msg)//向id是msg的标签，显示文字
            }
        },
        error:function(xhr,textStatus){//错误时执行该函数
            console.log('错误')
            console.log(xhr)
            console.log(textStatus)
        }
    })
}

function delById(id) {
    //询问框
    parent.layer.confirm('您确认删除该条记录吗？', {
        btn: ['确定','取消'], //按钮
        shade: false //不显示遮罩
    }, function(index){
        delByIdAjax(id)
        parent.layer.close(index)
    }, function(){
    });
}
function showById(id) {
    //iframe层
    parent.layer.open({
        type: 2,
        title: '修改商品信息',
        shadeClose: true,
        shade: 0.8,
        area: ['580px', '400px'],
        content: '/mgr/commodity/showById.html?id='+id, //iframe的url
        end:function() {
            // location.reload();
            doQuery()
        }
    });
}
function delByIdAjax(id){
    $.ajax({
        url:'/mgr/commodity/deleteById',
        type:'get',
        data:{
            id
        },
        dataType:'json',//服务器返回数据的类型
        success:function(data){//成功时执行该函数，data就是服务器返回的数据
            if(data.code==0){
                // location.reload()
                doQuery()
            }else{
                $("#msg").html(data.msg)//向id是msg的标签，显示文字
            }
        },
        error:function(xhr,textStatus){//错误时执行该函数
            console.log('错误')
            console.log(xhr)
            console.log(textStatus)
        }
    })
}